//
//  BDShopReceiveHongbaoCell.m
//  iOS_beboy
//
//  Created by wen on 2017/9/15.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDShopReceiveHongbaoCell.h"
#import "BDRedEnvelopeModel.h"

@interface BDShopReceiveHongbaoCell ()

@property (nonatomic, strong) UIView *bkView;
@property (nonatomic, strong) UIView *dashLine;
@property (nonatomic, strong) UIView *leftView;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, strong) YYLabel *moneyLabel;
@property (nonatomic, strong) BDRedEnvelopeModel *redEnvelope;

@end

@implementation BDShopReceiveHongbaoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    style = UITableViewCellStyleValue1;
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.bkView = [[UIView alloc] init];
        [self.contentView insertSubview:_bkView atIndex:0];
        self.dashLine = [[UIView alloc] init];
        [self.contentView addSubview:_dashLine];
        self.shapeLayer = [[CAShapeLayer alloc] init];
        [self.bkView.layer addSublayer:_shapeLayer];
        self.leftView = [[UIView alloc] init];
        [self.bkView addSubview:_leftView];
        self.moneyLabel = [YYLabel new];
        _moneyLabel.displaysAsynchronously = YES;
        _moneyLabel.ignoreCommonProperties = YES;
        [self.contentView addSubview:_moneyLabel];
        
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)configeWithModel:(id)model{
    self.redEnvelope = model;
    
    self.textLabel.text = [NSString stringWithFormat:ASLocalizedString(@"仅%@可用"), self.redEnvelope.shop.title];
    self.detailTextLabel.text = [NSString stringWithFormat:ASLocalizedString(@"满%@元可用"), [NSDecimalNumber decimalNumberWithString:self.redEnvelope.limit_money]];
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.bkView.size = CGSizeMake(self.width - 30, self.height);
    self.bkView.centerX = self.width / 2.0;
    
    self.shapeLayer.frame = self.bkView.bounds;
    
    CGFloat radius = 5;
    CGFloat wid = self.bkView.width / 3.0;
    
    UIBezierPath *bezierpath = [[UIBezierPath alloc] init];
    [bezierpath moveToPoint:CGPointMake(0, radius)];
    [bezierpath addLineToPoint:CGPointMake(0, self.height - radius)];
    [bezierpath addArcWithCenter:CGPointMake(radius, self.height - radius) radius:radius startAngle:M_PI endAngle:M_PI / 2.0 clockwise:NO];
    [bezierpath addLineToPoint:CGPointMake(wid - radius, self.height)];
    [bezierpath addArcWithCenter:CGPointMake(wid, self.height) radius:radius startAngle:M_PI endAngle:0 clockwise:YES];
    [bezierpath addLineToPoint:CGPointMake(self.bkView.width - radius, self.height)];
    [bezierpath addArcWithCenter:CGPointMake(self.bkView.width - radius, self.height - radius) radius:radius startAngle:M_PI / 2.0 endAngle:0 clockwise:NO];
    [bezierpath addLineToPoint:CGPointMake(self.bkView.width, radius)];
    [bezierpath addArcWithCenter:CGPointMake(self.bkView.width - radius, radius) radius:radius startAngle:0 endAngle:-M_PI / 2.0 clockwise:NO];
    [bezierpath addLineToPoint:CGPointMake(wid + radius, 0)];
    [bezierpath addArcWithCenter:CGPointMake(wid, 0) radius:radius startAngle:0 endAngle:M_PI clockwise:YES];
    [bezierpath addLineToPoint:CGPointMake(radius, 0)];
    [bezierpath addArcWithCenter:CGPointMake(radius, radius) radius:radius startAngle:-M_PI / 2.0 endAngle:-M_PI clockwise:NO];
    
    self.shapeLayer.path = bezierpath.CGPath;
    self.shapeLayer.fillColor = [UIColor whiteColor].CGColor;
    self.shapeLayer.shadowColor = [UIColor colorWithHexString:@"#cccccc"].CGColor;
    self.shapeLayer.shadowOffset = CGSizeMake(0, 0);
    self.shapeLayer.shadowOpacity = 1;
    self.shapeLayer.shadowRadius = 1;
    
    self.dashLine.size = CGSizeMake(1, self.height / 3.0);
    [self.dashLine drawLineLength:5 lineSpacing:2 lineColor:[UIColor colorWithHexString:@"#e5e5e5"] verticality:YES];
    self.dashLine.centerY = self.height / 2.0;
    self.dashLine.centerX = self.bkView.left + wid;
    
    self.leftView.size = CGSizeMake(5, self.bkView.height);
    self.leftView.left = 0;
    self.leftView.top = 0;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
        NSMutableAttributedString *text = [NSMutableAttributedString new];
        
        NSMutableAttributedString *emoText = [[NSMutableAttributedString alloc] initWithString:@"¥ "];
        emoText.font = [UIFont systemFontOfSize:14];
        emoText.color = [UIColor colorWithHexString:@"ff4d41"];
        [text appendAttributedString:emoText];
        
        NSMutableAttributedString *numberText = [[NSMutableAttributedString alloc] initWithString:self.redEnvelope.money];
        numberText.font = [UIFont systemFontOfSize:30];
        numberText.color = [UIColor colorWithHexString:@"ff4d41"];
        [text appendAttributedString:numberText];
        
        // Create text container
        YYTextContainer *container = [YYTextContainer new];
        container.size = CGSizeMake(100, CGFLOAT_MAX);
        container.maximumNumberOfRows = 0;
        
        // Generate a text layout.
        YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            _moneyLabel.size = layout.textBoundingSize;
            _moneyLabel.textLayout = layout;
            _moneyLabel.centerY = self.bkView.height / 2.0;
            _moneyLabel.centerX = self.bkView.width / 6.0 + self.bkView.left;
        });
    });
    
    self.textLabel.font = [UIFont systemFontOfSize:11];
    self.textLabel.textColor = [UIColor colorWithHexString:@"#808080"];
    [self.textLabel sizeToFit];
    self.textLabel.left = self.bkView.width / 3.0 + self.bkView.left + 20;
    self.textLabel.bottom = self.bkView.height / 2.0 - 5;
    
    self.detailTextLabel.font = [UIFont systemFontOfSize:11];
    self.detailTextLabel.textColor = [UIColor colorWithHexString:@"#808080"];
    [self.detailTextLabel sizeToFit];
    self.detailTextLabel.left = self.textLabel.left ;
    self.detailTextLabel.top = self.bkView.height / 2.0 + 5;
    
}

@end
